//
//  ProtocalVM.swift
//  kxf
//
//  Created by Mac on 2024/3/4.
//

import Foundation
import UIKit

class ProtocalVM: UIView {
    
    let selfHeight = kFitWidth(60)
    var isAgree = false
    
    var controller = WHBaseViewVC()

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var protocalSelectImg : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "protocal_normal_icon")
        img.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(agreeProtocalImgTapAction))
        img.addGestureRecognizer(tap)
        
        return img
    }()
    
    lazy var protocalLabel : YYLabel = {
        let lab = YYLabel()
        lab.isUserInteractionEnabled = true
        lab.numberOfLines = 2
        lab.lineBreakMode = .byCharWrapping
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.textColor = .COLOR_TEXT_BLACK333
        
        let attr = NSMutableAttributedString.init(string: "已充分阅读并遵守")
        let twoString = NSMutableAttributedString.init(string: "《隐私政策》")
        twoString.yy_color = .THEME
        let threeString = NSMutableAttributedString.init(string: "、")
        let fourString = NSMutableAttributedString.init(string: "《服务商合规展业规范》")
        fourString.yy_color = .THEME
        let fiveString = NSMutableAttributedString.init(string: "确保您的个人权益得到保护")
        
        twoString.yy_setTextHighlight(twoString.yy_rangeOfAll(), color: .THEME, backgroundColor: .white) { (view, text, range, rect) in
            DLLog(message: "《隐私政策》")
            let h5Vc = WHCommonH5VC()
            h5Vc.urlString = URL_H5_private as NSString
            h5Vc.modalPresentationStyle = .overFullScreen
            self.controller.present(h5Vc, animated: true)
        }
        fourString.yy_setTextHighlight(fourString.yy_rangeOfAll(), color: .THEME, backgroundColor: .white) { (view, text, range, rect) in
            DLLog(message: "《服务商合规展业规范》")
            let h5Vc = WHCommonH5VC()
            h5Vc.urlString = URL_H5_zhanye_guifan as NSString
            h5Vc.modalPresentationStyle = .overFullScreen
            self.controller.present(h5Vc, animated: true)
        }
        
        attr.append(twoString)
        if UserModel.shared.isCheck == false{
            attr.append(threeString)
            attr.append(fourString)
        }
        
        attr.append(fiveString)
        
        lab.attributedText = attr
        
        return lab
    }()
}

extension ProtocalVM{
    @objc func agreeProtocalImgTapAction() {
        isAgree = !isAgree
        if isAgree{
            protocalSelectImg.image = UIImage.init(named: "protocal_selected_icon")
        }else{
            protocalSelectImg.image = UIImage.init(named: "protocal_normal_icon")
        }
    }
}

extension ProtocalVM{
    func initUI() {
        addSubview(protocalSelectImg)
        addSubview(protocalLabel)
        
        setConstrait()
    }
    func setConstrait() {
        protocalSelectImg.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(35))
            make.top.equalTo(kFitWidth(15))
            make.width.height.equalTo(kFitWidth(22))
        }
        protocalLabel.snp.makeConstraints { make in
            make.left.equalTo(protocalSelectImg.snp.right).offset(kFitWidth(2))
            make.top.equalTo(protocalSelectImg).offset(kFitWidth(-2))
            make.right.equalTo(kFitWidth(-50))
            make.height.equalTo(kFitWidth(36))
        }
    }
}
